package com.nkdx.ws.controller;

import com.alibaba.fastjson.JSONObject;
import com.nkdx.ws.annotation.Log;
import com.nkdx.ws.dto.MeteorologicalDataDto;
import com.nkdx.ws.dto.RegionDto;
import com.nkdx.ws.eto.MeteorologicalDataEto;
import com.nkdx.ws.response.ApiResponse;
import com.nkdx.ws.service.MeteorologicalDataService;
import com.nkdx.ws.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *    气象数据
 * @author ZZW
 * @date 2022/3/4 6:48 下午
 * @param
 * @return
 */

@Controller
@RequestMapping("meteorologicalData")
public class MeteorologicalDataController {

    private RegionService regionService;
    private MeteorologicalDataService meteorologicalDataService;


    @Autowired
    public MeteorologicalDataController (RegionService regionService,MeteorologicalDataService meteorologicalDataService){
        this.regionService = regionService;
        this.meteorologicalDataService = meteorologicalDataService;
    }

    @RequestMapping("release")
    @ResponseBody
    public ApiResponse release(MeteorologicalDataEto eto){
        ApiResponse<Object> resp = new ApiResponse<>();
        MeteorologicalDataEto insert = meteorologicalDataService.release(eto);
        return resp.success(insert);
    }

    @Log(module = "气象数据管理",description = "去气象数据发布页面")
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<RegionDto> citys = regionService.findAllCity();
        model.addAttribute("citys",citys);
        return "meteorologicalData/meteorological_data_add";
    }

    @Log(module = "气象数据管理",description = "去气象数据修改页面")
    @RequestMapping("toUpdate")
    public String toUpdate(Model model,Integer id){
        List<RegionDto> citys = regionService.findAllCity();
        MeteorologicalDataEto eto = meteorologicalDataService.findById(id);
        model.addAttribute("citys",citys);
        model.addAttribute("eto",eto);

        return "meteorologicalData/meteorological_data_update";
    }


    @Log(module = "气象数据管理",description = "去气象数据管理页面")
    @RequestMapping("/toList")
    public String toList(){
        return "meteorologicalData/meteorological_data_list";
    }

    @RequestMapping("/toFind")
    public String toFind(Model model){
        List<RegionDto> citys = regionService.findAllCity();
        model.addAttribute("citys",citys);
        return "meteorologicalData/meteorological_data_find";
    }

    @Log(module = "气象数据管理",description = "发布气象数据信息")
    @RequestMapping("/insert")
    public String insert(MeteorologicalDataEto eto){
        meteorologicalDataService.insert(eto);
        return "meteorologicalData/meteorological_data_list";
    }

    @Log(module = "气象数据管理",description = "修改气象数据信息")
    @RequestMapping("/update")
    public String update(MeteorologicalDataEto eto){
        meteorologicalDataService.update(eto);
        return "meteorologicalData/meteorological_data_list";
    }

    @Log(module = "气象数据管理",description = "获取气象数据列表")
    @RequestMapping("findAll")
    @ResponseBody
    public ApiResponse findAll(){
        List<MeteorologicalDataDto> list = meteorologicalDataService.findAll();
        return new ApiResponse().success(list);
    }

    @Log(module = "气象数据管理",description = "根据城市获取气象数据列表")
    @RequestMapping("findAllByCity")
    @ResponseBody
    public ApiResponse findAllByCity(){
        List<MeteorologicalDataDto> list = meteorologicalDataService.findAllByCity();
        return new ApiResponse().success(list);
    }

    @Log(module = "气象数据管理",description = "根据Id删除气象数据")
    @RequestMapping("invalidById")
    @ResponseBody
    public ApiResponse invalidById(Integer id){
        meteorologicalDataService.invalidById(id);
        return new ApiResponse().success(new JSONObject());
    }

}
